import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '@/views/Login'
import Home from '@/views/Home'
import Users from '@/views/Users'
import Role from '@/views/Right/Role'
import Right from '@/views/Right/Right'
import Categories from '@/views/Good/Categories'
import Params from '@/views/Good/Params'
import Goods from '@/views/Good/Goods'
import GoodsAdd from '@/views/Good/GoodsAdd'
import Order from '@/views/Orders'


Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: "/login"// 重定向
  },
  {
    path: '/login',
    component: Login,
    name: 'login'
  },
  {
    path: '/home',
    component: Home,
    name: 'home',
    redirect:"/users",
    children:[
      {
        path:"/users",
        component:Users,
        name:"users"
      },
      {
        path:"/rights",
        component:Right,
        name:"rights"
      },
      {
        path:"/roles",
        component:Role,
        name:"roles"
      },
      {
        path:"/categories",
        component:Categories,
        name:"categories"
      },
      {
        path:"/params",
        component:Params,
        name:"params"
      },
      {
        path:"/goods",
        component:Goods,
        name:"goods"
      },
      {
        path:"/goods/add",
        component:GoodsAdd,
        name:"goodsadd"
      },
      {
        path:"/orders",
        component:Order,
        name:"order"
      }
      
    ]
  }
]

const router = new VueRouter({
  routes
})

// 导航守卫，没有登录不允许进入管理系统
router.beforeEach((to,from,next)=>{
  // 数组.includes(值) 返回布尔值
  if( ["/","/login"].includes(to.path) ){
    next()
  }else{
    // 判断是否登录了！
    if( sessionStorage.getItem("SHOP_USERINFO")){
      next()
    }else{
      next("/login")
    }
  }
})

export default router
